#include "type.h"
#include "const.h"
#include "protectmode.h"
#include "prototype.h"
#include "string.h"
#include "global.h"


extern int32 memery_num;

//内核初始化硬件
PUBLIC void init()
{
		
		disp_str("Initializing Hardware...\n");
		init_gdt_and_idt();
		init_prot();
		init_8253();

		
		disp_str("Available Memory:");
		disp_int_dec(memery_num);
		disp_str(" MB\n");
		init_keyboard();
}

//初始化gdt和idt
PUBLIC void init_gdt_and_idt()
{

	disp_str("Setup gdt and idt...\n");
	memcpy( &gdt,(void*)(*((int32*)(&gdt_ptr[2]))),*((int16*)(&gdt_ptr[0])) + 1);
	
	int16* p_gdt_limit = (int16*)(&gdt_ptr[0]);
	int32* p_gdt_base = (int32*)(&gdt_ptr[2]);
	*p_gdt_limit = GDT_SIZE * sizeof(DESCRIPTOR) - 1;
	*p_gdt_base = (int32)&gdt;
		
		
	int16* p_idt_limit = (int16*)(&idt_ptr[0]);
	int32* p_idt_base = (int32*)(&idt_ptr[2]);
	*p_idt_limit = IDT_SIZE * sizeof(GATE) - 1;
	*p_idt_base = (int32)&idt;
}

//初始化8253
PUBLIC void init_8253()
{
	out_byte(TIMER_MODE, RATE_GENERATOR);
	out_byte(TIMER0, (int8) (TIMER_FREQ/HZ) );
	out_byte(TIMER0, (int8) ((TIMER_FREQ/HZ) >> 8));
}

